package leetcode;

import java.util.concurrent.Callable;

interface Reptile {
    ReptileEgg lay();
}

class FireDragon implements Reptile{
    public FireDragon() {
    }

    public static void main(String[] args) throws Exception {
        FireDragon fireDragon = new FireDragon();
        System.out.println(fireDragon instanceof Reptile);
    }


    @Override
    public ReptileEgg lay() {
        return new ReptileEgg(new Callable<Reptile>() {
            @Override
            public Reptile call() throws Exception {
                return new FireDragon();
            }
        });
    }
}

class ReptileEgg {
    public ReptileEgg(Callable<Reptile> createReptile) {
        try {
            createReptile.call();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private volatile boolean ifUnhatched = true;
    public Reptile hatch() throws Exception {
        if(!ifUnhatched){
            throw new IllegalStateException();
        }
        ifUnhatched = true;
        return new Reptile() {
            @Override
            public ReptileEgg lay() {
                return null;
            }
        };
    }
}